// pages/fwdp/fwdp.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    type:'',
    tcode:'',
    upimgbtn:true,
    pics: [],
    urlimg: "",
    pimgBox:[],
    isdisabled:false,
    stars:[
      {
        flag:1,
        bgImg: "../../static/image/starn.png",
        bgfImg:"../../static/image/starn.png"
      },
      {
        flag: 1,
        bgImg: "../../static/image/starn.png",
        bgfImg:"../../static/image/star1.png"
      },
      {
        flag: 1,
        bgImg: "../../static/image/starn.png",
        bgfImg:"../../static/image/star1.png"
      },
      {
        flag: 1,
        bgImg: "../../static/image/starn.png",
        bgfImg:"../../static/image/star1.png"
      },
      {
        flag: 1,
        bgImg: "../../static/image/starn.png",
        bgfImg:"../../static/image/star1.png"
      }
    ],
    star_num: 0,
  },
  dpContent(e){
    var code = e.currentTarget.dataset.type;
    var type = '';
    if(code=='ctfw'){
      type='餐厅服务';
    }else if(code=='lffw'){
      type='理发服务';
    }else if(code=='xyfw'){
      type='洗浴服务';
    }
    for(var i=0;i<5;i++){
      var item = 'stars['+i+'].flag';
      this.setData({
        [item]: 1
      })
    }
    this.setData({
      showModal: true,
      type: type,
      tcode: code,
      ['stars[0]']:{
        flag:0,
        bgImg: "../../static/image/starn.png",
        bgfImg:"../../static/image/starn.png"
      }
    })
  },
  gb(){
    for(var i=0;i<this.data.star_num;i++){
      var item = 'stars['+i+'].flag';
      this.setData({
        [item]: 1
      })
    }
    this.setData({
      showModal:false,
      upimgbtn:true,
      isdisabled:false,
      pics: [],
      urlimg: "",
      star_num: 0,
      pimgBox:[]
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  score:function(e){
    var that=this;
    if(that.data.stars[0].flag==0){
      this.setData({
        ['stars[0]']:{
            flag:1,
            bgImg: "../../static/image/starn.png",
            bgfImg:"../../static/image/star1.png"
        }
      })
    }
    for(var i=0;i<that.data.stars.length;i++){
      var allItem = 'stars['+i+'].flag';
      that.setData({
        [allItem]: 1
      })
    }
    var index=e.currentTarget.dataset.index;
    this.setData({
      star_num: index+1
    })
    for(var i=0;i<=index;i++){
      var item = 'stars['+i+'].flag';
      that.setData({
        [item]: 2
      })
    }
  },
  upShopLogo: function() {
		var that = this;
		wx.showActionSheet({
			itemList: ['拍摄','从相册中选择'],
			itemColor: "black",
			success: function(res) {
				if (!res.cancel) {
					if (res.tapIndex == 1) {
						that.chooseWxImageShop('album')
          }else if (res.tapIndex == 0) {
						that.chooseWxImageShop('camera')
          }
				}
			}
		})
	},
 
	chooseWxImageShop: function(type) {
    var that = this;
		wx.chooseImage({
      count: 3-this.data.pics.length, // 可选择的图片数量
			sizeType: ['compressed'],
			sourceType: [type],
			success: function(res) {
        that.setData({
          pimgBox:res.tempFiles
        })
				var pics = that.data.pics;
        let urlimg = pics.concat(res.tempFilePaths);
        if (res.tempFilePaths.length > 3) {
					wx.showToast({
            title: '最多上传3张',
            icon: 'error',
            duration: 2e3//持续的时间
          });
					return false
				}
				if (res.tempFilePaths.length <= 3) {
          that.setData({
            pics: urlimg,
            urlimg: urlimg
          })
          if(urlimg.length == 3){
            that.setData({
              upimgbtn:false
            })
          }
				} else {
					wx.showToast({
            title: '最多上传3张',
            icon: 'error',
            duration: 2e3//持续的时间
          });
				}
			}
		})
  },
  deleteImg: function (e) {
    var imgs = this.data.pics;
    var index = e.currentTarget.dataset.index;
    var temp = this.data.pimgBox;
    imgs.splice(index, 1);
    temp.splice(index, 1);
    this.setData({
      pics: imgs,
      upimgbtn: true,
      pimgBox: temp,
      beater:this.data.beater-1
    });

  },
  formSubmit: function (e){
    var value = e.detail.value;
    var that = this;
    if(value.comments.length == 0){
      wx.showToast({
        title: '请填写评价内容！',
        icon: "none",
        duration: 2e3//持续的时间
      });
      return false;
    }
    if(value.comments.length >= 200){
      wx.showToast({
        title: '评价内容不能超过两百字！',
        icon: "none",
        duration: 2e3//持续的时间
      });
      return false;
    }
    if(that.data.star_num == 0){
      wx.showToast({
        title: '请对服务进行评分！',
        icon: "none",
        duration: 2e3//持续的时间
      });
      return false;
    }
    const FormData = require('../../formData.js');
    const form =  new FormData();
    const fs = wx.getFileSystemManager();
    for(let i = 0;i<that.data.pimgBox.length;i++){
      form.appendFile("file", that.data.pimgBox[i].path, "t");
    }
    form.append("dp",value.comments);
    form.append("star",that.data.star_num);
    form.append("lx",that.data.tcode);

    let data = form.getData();
    wx.request({
      method: "POST",
      url: "http://42.228.11.30:41024/fwdp/saveDp",
      header: {
        'content-type': data.contentType
      },
      data: data.buffer,
      success: res => {
        if(res.data == 1){
          that.setData({
            isdisabled:true
          })
          wx.showToast({
            title: '感谢您的点评',
            icon: 'none',
            duration: 2e3,//持续的时间
            success:function(){
              setTimeout(function () { 
                that.setData({
                  upimgbtn:true,
                  pics: [],
                  urlimg: "",
                  pimgBox:[],
                  star_num: 0
                })
                that.gb();
            }, 1500)
            }
          })
          // wx.showModal({
          //   title: '点评成功',
          //   content: '请选择继续点评还是返回首页',
          //   showCancel: true,//是否显示取消按钮
          //   cancelText:"继续点评",//默认是“取消”
          //   cancelColor:'#f7982a',//取消文字的颜色
          //   confirmText:"返回首页",//默认是“确定”
          //   confirmColor: '#f7982a',//确定文字的颜色
          //   success: function (res) {
          //      if (res.cancel) {
          //         //刷新本页面
          //         that.onLoad()
          //      } else {
          //         //点击确定
          //         wx.navigateBack({
          //           delta: 2
          //         })
          //      }
          //   },
          //   fail: function (res) { },//接口调用失败的回调函数
          //   complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
        //  })
        }else{
          wx.showToast({
            title: '提交失败',
            icon: 'none',
            duration: 2e3,//持续的时间
          })
        }
      }
    });
  },
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})